//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.common.JobConsts;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssTranSendDao;
import com.aidong.model.wills.PssTranSendImpl;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.web.controller.BaseController;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"purcTranSendRpt"})
public class PurcTranSendRptController extends BaseController {
    @Resource
    PssTranSendDao pssTranSendDao;

    public PurcTranSendRptController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("pss/transendrpt/list");
        mv.addObject("shopName", this.getTypeList("shopName"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "ReceiveShop", shopCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("SendDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("SendDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.pssTranSendDao.getTranSendRptList(pg, cd);
        ModelAndView mv = new ModelAndView("pss/tranSendRpt/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("ProductcodeList", this.getTypeList("ProductcodeList"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"_excelexport"})
    public String _excelexport(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletRequest request, HttpServletResponse response) {
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "ReceiveShop", shopCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("SendDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("SendDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        List lst = this.pssTranSendDao.getExcelTranSendRptList(cd);
        Iterator data = lst.iterator();

        while(data.hasNext()) {
            PssTranSendImpl ts = (PssTranSendImpl)data.next();
            ts.setUnitName(this.cache.getValue("productUnit", ts.getUnit()));
        }

        HashMap data1 = new HashMap();
        data1.put("list", lst);
        Timestamp ts1 = DateUtil.getCurrentTimestamp();
        String cur = DateUtil.formatDate(ts1, DateUtil.DATE_PATTERN_2);
        String fileName = cur + "_" + JobConsts.TEMPLATE_NAME_TRAN_SEND + JobConsts.EXCEL_SUFFIX;
        String fileChName = cur + "_" + JobConsts.CH_NAME_TRAN_SEND + JobConsts.EXCEL_SUFFIX;
        ExportUtil.exportExcel(this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY), fileName, this.getSetting(JobConsts.EXCEL_TEMPLATE_PATH_KEY), JobConsts.TEMPLATE_NAME_TRAN_SEND + JobConsts.EXCEL_SUFFIX, data1);
        this.download(fileName, fileChName, request, response);
        return null;
    }

    private String download(String fileName, String fileChName, HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream os = null;
        FileInputStream is = null;

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileChName.getBytes(), "iso-8859-1"));
            String e2 = this.getSetting(JobConsts.EXCEL_OUT_PATH_KEY);
            is = new FileInputStream(new File(e2 + File.separator + fileName));
            os = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }

            os.close();
            is.close();
        } catch (FileNotFoundException var25) {
            var25.printStackTrace();
        } catch (IOException var26) {
            var26.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException var24) {
                    ;
                }
            }

            if(is != null) {
                try {
                    is.close();
                } catch (IOException var23) {
                    ;
                }
            }

        }

        return null;
    }
}
